package com.example.designPattern.Prototype;

import java.io.*;


public class DeepClone implements Serializable,Cloneable {

    private String name;//名字
    private String moblie; //手机号码
    private Address address;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMoblie() {
        return moblie;
    }

    public void setMoblie(String moblie) {
        this.moblie = moblie;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public DeepClone(String name, String moblie, Address address) {
        this.name = name;
        this.moblie = moblie;
        this.address = address;
    }

    public DeepClone() {
    }

    public DeepClone clone() throws CloneNotSupportedException {
        DeepClone proto = null;
        try {
            proto = deepClone();
            proto.address= address.deepClone();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return proto;
    }


    public DeepClone deepClone() throws IOException, ClassNotFoundException, OptionalDataException {

        //将对象写入流中
        ByteArrayOutputStream bao=new  ByteArrayOutputStream();

        ObjectOutputStream oos=new  ObjectOutputStream(bao);

        oos.writeObject(this);

        //将对象从流中取出

        ByteArrayInputStream bis=new  ByteArrayInputStream(bao.toByteArray());

        ObjectInputStream ois=new  ObjectInputStream(bis);

        return  (DeepClone)ois.readObject();
    }
}
